package cn.johnyu.demo;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet(name = "loginServlet", value = "/login")
public class LoginServlet extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        if("admin".equals(username)&&"123".equals(password)) {
            req.getSession().setAttribute("user", username);
            req.removeAttribute("msg");
            req.getRequestDispatcher("/WEB-INF/jsp/success.jsp").forward(req, resp);
        }
        else {
            req.setAttribute("msg", "用户名或密码无效！");
            req.getRequestDispatcher("/index.jsp").forward(req, resp);
        }
    }


}
